package reno.alerts.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reno.alerts.common.HttpGetTool;
import reno.alerts.common.JSONUtils;
import reno.alerts.mo.ResponseMO;
import reno.alerts.service.AlertsService;

import java.io.IOException;
import java.util.List;

@RestController
public class AlertsController {

    @Autowired
    AlertsService alertsService;

    @RequestMapping("/getAlertsInfo")
    public JSONObject getAlertsInfo() {
        System.out.println();
        String url = "https://api.warframestat.us/pc/alerts";
        String alertsString;
        try {
            alertsString = HttpGetTool.getResult(url);
        } catch (IOException e) {
            return JSONUtils.failure("Alerts Info GET Error,Please Try Again");
        }
        List<ResponseMO> responseMOList = alertsService.parseList(alertsString);
        String result = alertsService.explain(responseMOList);

        return JSONUtils.success(result);
    }

}
